//
//  UIButton+SPRelayout.m
//  EMarket
//
//  Created by 石头 on 2018/10/16.
//  Copyright © 2018 好日子. All rights reserved.
//

#import "UIButton+SPRelayout.h"

@implementation UIButton (SPRelayout)

- (void)relayoutButton:(SPButtonRelayoutType)type {
    [self layoutIfNeeded];
    
    CGRect titleFrame = self.titleLabel.frame;
    CGRect imageFrame = self.imageView.frame;
    
    CGFloat space = titleFrame.origin.x - imageFrame.origin.x - imageFrame.size.width;
    
    if (type == SPButtonRelayoutType_left) {
        [self setImageEdgeInsets:UIEdgeInsetsMake(0, titleFrame.size.width + space, 0, -(titleFrame.size.width + space))];
        [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -(titleFrame.origin.x - imageFrame.origin.x), 0, titleFrame.origin.x - imageFrame.origin.x)];
    } else if(type == SPButtonRelayoutType_bottom) {
        [self setImageEdgeInsets:UIEdgeInsetsMake(0,0, titleFrame.size.height + space, -(titleFrame.size.width))];
        [self setTitleEdgeInsets:UIEdgeInsetsMake(imageFrame.size.height + space, -(imageFrame.size.width), 0, 0)];
    }
}

@end
